package SynchronizedStudy;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-11-24
 * Time: 9:49
 */
public class synchronizedTestDemo1 {

    public static int counter = 0;//共享变量counter
    public static Object locker = new Object();//共享的锁对象

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for(int i =0;i<5000;++i){
                synchronized (locker){//加锁
                    //临界区
                    counter++;
                }
            }
        });
        Thread t2 = new Thread(()->{
            for(int i =0;i<5000;++i){
                //临界区
                counter--;
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter);
    }


}
